package com.example.mvptest;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.mvptest.model.HandleModel;
import com.example.mvptest.presenter.IPresenter;
import com.example.mvptest.presenter.Presenter;

public class MainActivity extends Activity implements IView {

    private EditText edit;
    private TextView titleText;
    private TextView msgText;
    private Button clearText;


    Presenter presenter = new  Presenter();
    HandleModel model = new HandleModel();
    IPresenter mpresenter  = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        titleText = (TextView)findViewById(R.id.titleText);
        edit = (EditText)findViewById(R.id.edit);
        msgText = (TextView)findViewById(R.id.msgText);
        clearText = (Button)findViewById(R.id.clearText);

        this.setPresenter(presenter);
        presenter.setView(this);
        model.setPresenter(presenter);
        presenter.setModel(model);
        mpresenter = presenter;

        titleText.setText("MVP");
        edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
            @Override
            public void afterTextChanged(Editable s) {
                mpresenter.onTextChanged(s.toString());
            }
        });

        clearText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mpresenter.onClearBtnClicked();
            }
        });
    }

    @Override
    public void setPresenter(IPresenter presenter) {
        this.mpresenter = presenter;
    }

    @Override
    public void loading() {
        msgText.setText("handling data ...");
    }

    @Override
    public void showData(String data) {
        msgText.setText(data);
    }
}